package com.studyyoun.android.nonghe.home.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.studyyoun.android.androidcommonbaselibrary.activity.CommonBaseActivity;
import com.studyyoun.android.androidcommonbaselibrary.bottombar.AnimationBottomBar;
import com.studyyoun.android.androidcommonbaselibrary.bottombar.BottomItem;
import com.studyyoun.android.androidcommonbaselibrary.fragment.CommonBaseFragment;
import com.studyyoun.android.androidcommonbaselibrary.utils.LogUtils;
import com.studyyoun.android.nonghe.R;
import com.studyyoun.android.nonghe.home.fragment.FindFragment;
import com.studyyoun.android.nonghe.home.fragment.HomeFragment;
import com.studyyoun.android.nonghe.home.fragment.MsgFragment;
import com.studyyoun.android.nonghe.home.fragment.PersonFragment;
import com.studyyoun.android.nonghe.welcom.CommonUserHelper;
import com.studyyoun.android.nonghe.welcom.activity.LoginActivity;
import com.studyyoun.android.nonghe.welcom.model.UserModel;

/**
 * Created by androidlongs on 2017/8/27.
 * 站在顶峰，看世界
 * 落在谷底，思人生
 */

public class HomeActivity extends CommonBaseActivity {
    private android.widget.FrameLayout mFlHomeContent;
    private android.widget.LinearLayout mLlHomeTabHome;
    private android.widget.ImageView mIvHomeTabHome;
    private android.widget.TextView mTvHomeTabHome;
    private android.widget.LinearLayout mLlHomeTabMsg;
    private android.widget.ImageView mIvHomeTabMsg;
    private android.widget.TextView mTvHomeTabMsg;
    private android.widget.LinearLayout mLlHomeTabFind;
    private android.widget.ImageView mIvHomeTabFind;
    private android.widget.TextView mTvHomeTabFind;
    private android.widget.LinearLayout mLlHomeTabMy;
    private android.widget.ImageView mIvHomeTabMy;
    private android.widget.TextView mTvHomeTabMy;
    private CommonBaseFragment mHomeFragment;
    private CommonBaseFragment mFindFragment;
    private CommonBaseFragment mMsgFragment;
    private CommonBaseFragment mPersonFragment;
    private HomeReciver mHomeReciver;
    private LinearLayout mRootLinearLayout;
    private AnimationBottomBar animationBottomBar;

    @Override
    protected void getAllIntentExtraDatas(Intent intent) {

    }

    @Override
    protected int getCommonLayoutId() {
        return R.layout.activity_home;
    }

    @Override
    protected void commonInitView(View view) {
        initView();
    }

    @Override
    protected void commonFunction() {


    }


    @Override
    protected void commonDelayFunction() {

        initTableFragment();

        setTabBarClickListerner();
        //默认显示页面
        setTabBarStatue(1);


        IntentFilter intentFilter = new IntentFilter("show_statue");
        intentFilter.addAction("hidd_statue");
        mHomeReciver = new HomeReciver();
        registerReceiver(mHomeReciver, intentFilter);
    }

    private void initTableFragment() {
        FragmentManager lFragmentManager = this.getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = lFragmentManager.beginTransaction();
        if (mHomeFragment == null) {
            mHomeFragment = new HomeFragment();
            fragmentTransaction.add(R.id.fl_home_content, mHomeFragment, "lHomeFragment");
            fragmentTransaction.show(mHomeFragment);
        }
        if (mMsgFragment == null) {
            mMsgFragment = new MsgFragment();
            fragmentTransaction.add(R.id.fl_home_content, mMsgFragment, "lMsgFragment");
            fragmentTransaction.hide(mMsgFragment);
        }
        if (mFindFragment == null) {
            mFindFragment = new FindFragment();
            fragmentTransaction.add(R.id.fl_home_content, mFindFragment, "lFindFragment");
            fragmentTransaction.hide(mFindFragment);
        }
        if (mPersonFragment == null) {
            mPersonFragment = new PersonFragment();
            fragmentTransaction.add(R.id.fl_home_content, mPersonFragment, "lPersonFragment");
            fragmentTransaction.hide(mPersonFragment);
        }
        fragmentTransaction.commit();
    }

    private void initView() {

        mRootLinearLayout = findViewById(R.id.ll_home_main_roor_view);
        mFlHomeContent = (FrameLayout) findViewById(R.id.fl_home_content);
        mLlHomeTabHome = (LinearLayout) findViewById(R.id.ll_home_tab_home);
        mIvHomeTabHome = (ImageView) findViewById(R.id.iv_home_tab_home);
        mTvHomeTabHome = (TextView) findViewById(R.id.tv_home_tab_home);
        mLlHomeTabMsg = (LinearLayout) findViewById(R.id.ll_home_tab_msg);
        mIvHomeTabMsg = (ImageView) findViewById(R.id.iv_home_tab_msg);
        mTvHomeTabMsg = (TextView) findViewById(R.id.tv_home_tab_msg);
        mLlHomeTabFind = (LinearLayout) findViewById(R.id.ll_home_tab_find);
        mIvHomeTabFind = (ImageView) findViewById(R.id.iv_home_tab_find);
        mTvHomeTabFind = (TextView) findViewById(R.id.tv_home_tab_find);
        mLlHomeTabMy = (LinearLayout) findViewById(R.id.ll_home_tab_my);
        mIvHomeTabMy = (ImageView) findViewById(R.id.iv_home_tab_my);
        mTvHomeTabMy = (TextView) findViewById(R.id.tv_home_tab_my);

        animationBottomBar = findViewById(R.id.animation_bar);

        try {
            animationBottomBar.addItem(new BottomItem(R.mipmap.tab_home_normal, R.mipmap.tab_home_press, "首页", 20, Color.parseColor("#aaf6f5ec")))
                    .addItem(new BottomItem(R.mipmap.home_table_msg_normal, R.mipmap.home_table_msg_select, "检测记录", 20, Color.parseColor("#aaeeFFFF")))
                    .addItem(new BottomItem(R.mipmap.home_table_me_normal, R.mipmap.home_table_me_select, "资金发放", 20, Color.parseColor("#aaf6ffec")))
                    .addItem(new BottomItem(R.mipmap.home_table_topic_normal, R.mipmap.home_table_topic_select, "我的资料", 20, Color.parseColor("#aaF0F8FF")))
                    .build();
        } catch (Exception e) {
            e.printStackTrace();
        }

        animationBottomBar.setItemSelectListener(new AnimationBottomBar.OnItemSelectListener() {
            @Override
            public void onItemSelectListener(int position) {
                LogUtils.d("onItemSelectListener: position" + position);
                switch (position) {
                    case 0:
                        selectFragmentFunction(1);
                        break;
                    case 1:
                        selectFragmentFunction(2);
                        break;
                    case 2:
                        selectFragmentFunction(3);
                        break;
                    case 3:
                        selectFragmentFunction(4);
                        break;
                }
            }
        });
    }

    private void setTabBarClickListerner() {

        mLlHomeTabHome.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setTabBarStatue(1);
                selectFragmentFunction(1);
            }
        });
        mLlHomeTabMsg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setTabBarStatue(2);
                selectFragmentFunction(2);
            }
        });
        mLlHomeTabFind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setTabBarStatue(3);
                selectFragmentFunction(3);
            }
        });
        mLlHomeTabMy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setTabBarStatue(4);
                selectFragmentFunction(4);
            }
        });
    }

    private void setTabBarStatue(int i) {
        switch (i) {
            case 1:
                mIvHomeTabHome.setImageResource(R.mipmap.tab_home_press);
                mTvHomeTabHome.setTextColor(getResources().getColor(R.color.HomeTabBarTextSelectColor));

                mIvHomeTabMsg.setImageResource(R.mipmap.home_table_msg_normal);
                mTvHomeTabMsg.setTextColor(getResources().getColor(R.color.HomeTabBarTextNormalColor));

                mIvHomeTabFind.setImageResource(R.mipmap.home_table_topic_normal);
                mTvHomeTabFind.setTextColor(getResources().getColor(R.color.HomeTabBarTextNormalColor));

                mIvHomeTabMy.setImageResource(R.mipmap.home_table_me_normal);
                mTvHomeTabMy.setTextColor(getResources().getColor(R.color.HomeTabBarTextNormalColor));

                break;
            case 2:
                mIvHomeTabHome.setImageResource(R.mipmap.tab_home_normal);
                mTvHomeTabHome.setTextColor(getResources().getColor(R.color.HomeTabBarTextNormalColor));

                mIvHomeTabMsg.setImageResource(R.mipmap.home_table_msg_select);
                mTvHomeTabMsg.setTextColor(getResources().getColor(R.color.HomeTabBarTextSelectColor));

                mIvHomeTabFind.setImageResource(R.mipmap.home_table_topic_normal);
                mTvHomeTabFind.setTextColor(getResources().getColor(R.color.HomeTabBarTextNormalColor));

                mIvHomeTabMy.setImageResource(R.mipmap.home_table_me_normal);
                mTvHomeTabMy.setTextColor(getResources().getColor(R.color.HomeTabBarTextNormalColor));
                break;
            case 3:
                mIvHomeTabHome.setImageResource(R.mipmap.tab_home_normal);
                mTvHomeTabHome.setTextColor(getResources().getColor(R.color.HomeTabBarTextNormalColor));

                mIvHomeTabMsg.setImageResource(R.mipmap.home_table_msg_normal);
                mTvHomeTabMsg.setTextColor(getResources().getColor(R.color.HomeTabBarTextNormalColor));

                mIvHomeTabFind.setImageResource(R.mipmap.home_table_topic_select);
                mTvHomeTabFind.setTextColor(getResources().getColor(R.color.HomeTabBarTextSelectColor));

                mIvHomeTabMy.setImageResource(R.mipmap.home_table_me_normal);
                mTvHomeTabMy.setTextColor(getResources().getColor(R.color.HomeTabBarTextNormalColor));
                break;
            case 4:
                mIvHomeTabHome.setImageResource(R.mipmap.tab_home_normal);
                mTvHomeTabHome.setTextColor(getResources().getColor(R.color.HomeTabBarTextNormalColor));

                mIvHomeTabMsg.setImageResource(R.mipmap.home_table_msg_normal);
                mTvHomeTabMsg.setTextColor(getResources().getColor(R.color.HomeTabBarTextNormalColor));

                mIvHomeTabFind.setImageResource(R.mipmap.home_table_topic_normal);
                mTvHomeTabFind.setTextColor(getResources().getColor(R.color.HomeTabBarTextNormalColor));

                mIvHomeTabMy.setImageResource(R.mipmap.home_table_me_select);
                mTvHomeTabMy.setTextColor(getResources().getColor(R.color.HomeTabBarTextSelectColor));
                break;
        }
    }

    private void selectFragmentFunction(int flag) {

        FragmentManager lFragmentManager = this.getSupportFragmentManager();
        mHomeFragment = (CommonBaseFragment) lFragmentManager.findFragmentByTag("lHomeFragment");
        mFindFragment = (CommonBaseFragment) lFragmentManager.findFragmentByTag("lFindFragment");
        mMsgFragment = (CommonBaseFragment) lFragmentManager.findFragmentByTag("lMsgFragment");
        mPersonFragment = (CommonBaseFragment) lFragmentManager.findFragmentByTag("lPersonFragment");


        FragmentTransaction lTransaction = lFragmentManager.beginTransaction();


        switch (flag) {
            case 1:
                if (mHomeFragment == null) {
                    mHomeFragment = new HomeFragment();
                    lTransaction.add(R.id.fl_home_content, mHomeFragment, "lHomeFragment");
                    lTransaction.show(mHomeFragment);
                } else {
                    lTransaction.show(mHomeFragment);
                }
                if (mFindFragment != null) {
                    lTransaction.hide(mFindFragment);
                }
                if (mMsgFragment != null) {
                    lTransaction.hide(mMsgFragment);
                }
                if (mPersonFragment != null) {
                    lTransaction.hide(mPersonFragment);
                }
                break;
            case 2:

                UserModel userModel = CommonUserHelper.getUserModel();
                if (userModel != null) {
                    UserModel.DataBean data = userModel.getData();
                    if (data != null) {
                        Intent lIntent = new Intent(mContext,MoreCheckStatueActivity.class);
                        lIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        mContext.startActivity(lIntent);
                        return;
                    }
                }

                Intent lIntent = new Intent(mContext,LoginActivity.class);
                lIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                mContext.startActivity(lIntent);
                break;
            case 3:
//                if (mFindFragment == null) {
//                    mFindFragment = new FindFragment();
//                    lTransaction.add(R.id.fl_home_content, mFindFragment, "lFindFragment");
//                    lTransaction.hide(mFindFragment);
//                } else {
//                    lTransaction.show(mFindFragment);
//                }
//                if (mHomeFragment != null) {
//                    lTransaction.hide(mHomeFragment);
//                }
//                if (mMsgFragment != null) {
//                    lTransaction.hide(mMsgFragment);
//                }
//                if (mPersonFragment != null) {
//                    lTransaction.show(mPersonFragment);
//                }else{
//                    mPersonFragment = new PersonFragment();
//                    lTransaction.add(R.id.fl_home_content, mFindFragment, "lPersonFragment");
//                    lTransaction.show(mPersonFragment);
//                }
//
//                ((FindFragment) mFindFragment).loadingNetDatasFunction();

                UserModel userModel2 = CommonUserHelper.getUserModel();
                if (userModel2 != null) {
                    UserModel.DataBean data = userModel2.getData();
                    if (data != null) {
                        Intent lIntent2 = new Intent(mContext,MornyActivity.class);
                        lIntent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        mContext.startActivity(lIntent2);
                        return;
                    }
                }

                Intent lInten2t = new Intent(mContext,LoginActivity.class);
                lInten2t.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                mContext.startActivity(lInten2t);

                break;
            case 4:

//
//                if (mFindFragment == null) {
//                    mFindFragment = new FindFragment();
//                    lTransaction.add(R.id.fl_home_content, mFindFragment, "lFindFragment");
//                    lTransaction.show(mFindFragment);
//                } else {
//                    lTransaction.show(mFindFragment);
//                }
//                if (mHomeFragment != null) {
//                    lTransaction.hide(mHomeFragment);
//                }
//                if (mMsgFragment != null) {
//                    lTransaction.hide(mMsgFragment);
//                }
//                if (mPersonFragment != null) {
//                    lTransaction.hide(mPersonFragment);
//                }
//
//                ((FindFragment) mFindFragment).loadingNetDatasFunction();

                UserModel userModel3 = CommonUserHelper.getUserModel();
                if (userModel3 != null) {
                    UserModel.DataBean data = userModel3.getData();
                    if (data != null) {
                        Intent lIntent2 = new Intent(mContext,MyInformationActivity.class);
                        lIntent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        mContext.startActivity(lIntent2);
                        return;
                    }
                }

                Intent intent3 = new Intent(mContext,LoginActivity.class);
                intent3.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                mContext.startActivity(intent3);

                break;
        }

        lTransaction.commit();
    }


    @Override
    protected void onResume() {
        super.onResume();
        animationBottomBar.setSelectIndex(0);
        setTabBarStatue(1);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mHomeReciver != null) {
            unregisterReceiver(mHomeReciver);
        }
    }

    public class HomeReciver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();

            LogUtils.d("statue " + action);
            if (TextUtils.equals(action, "show_statue")) {
                mRootLinearLayout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
            } else if (TextUtils.equals(action, "hidd_statue")) {
                mRootLinearLayout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);


            }

        }
    }
}
